package com.yd.cfckc.listener;

import com.yd.cfckc.task.QlDataTask;
import com.yd.cfckc.task.SjdwTask;
import com.yd.cfckc.util.RedisPool;
import com.yd.cfckc.util.SrvUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;

/**
 * @author cuiyufeng
 * @email cuiyufeng1458@yundasys.com
 * @date 2020/10/16 4:14 下午
 */
@WebListener
@Component
public class ContextListener implements ServletContextListener {

    @Value("${redishost}")
    private String redisHost;

    @Value("${redisport}")
    private Integer redisPort;

    Timer timer = null;

    /**
     * TODO 销毁定时器.上午9:46:39
     * Date: 2018年6月7日 上午9:46:39
     */
    @Override
    public void contextDestroyed(ServletContextEvent event) {
       timer.cancel();
        event.getServletContext().log("定时器已销毁");
    }

    /**
     * TODO 启动定时器.上午9:47:00
     * Date: 2018年6月7日 上午9:47:00
     */
    @Override
    public void contextInitialized(ServletContextEvent event) {
        RedisPool.defhost = SrvUtil.getProperties().getProperty(
                "redishost");
        RedisPool.defPort = Integer.parseInt(SrvUtil
                .getProperties().getProperty("redisport"));
        RedisPool.initPool();
        timer = new Timer();
        event.getServletContext().log("定时器已经启动");
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        System.out.println(calendar.getTime());
        // 从date时刻开始每隔1200000毫秒执行一次
        // 从date时刻开始每隔43200000毫秒执行一次
        timer.schedule(new QlDataTask(), date, 120000L);
        timer.schedule(new SjdwTask(), date, 43200000L);
        event.getServletContext().log("错分校验数据更新---已经添加任务调度表");
    }
}
